package com.example.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.example.pojo.ValidateCode;
import com.example.service.SessionService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.UUID;


@Service
public class SessionServiceImpl implements SessionService {
    @Override
    public ValidateCode generateValidateCode(HttpServletRequest request) {
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(100, 40, 4, 10);
        String codeValue = circleCaptcha.getCode();
        String imageBase64 = circleCaptcha.getImageBase64();

        String codeKey = UUID.randomUUID().toString().replaceAll("-", "");

        HttpSession session = request.getSession();
        session.setAttribute("emp:login:" + codeKey,codeValue);

        ValidateCode validateCode = new ValidateCode();
        validateCode.setCodeKey(codeKey);
        validateCode.setCodeValue("data:image/png;base64," + imageBase64);
        return validateCode;
    }
}
